<template>
  <Modal v-model="modal.show" width="450" :title="modal.title">
    <Form ref="form" :model="formItem" :rules="ruleValidate" :label-width="150">
      <FormItem label="DRG编码" prop="drg_code"><Input v-model="formItem.drg_code" placeholder="" style="width: 200px" />
      </FormItem>

      <FormItem label="DRG名称" prop="drg_name"><Input v-model="formItem.drg_name" placeholder="" style="width: 200px" />
      </FormItem>

      <FormItem label="权重" prop="qz">
        <InputNumber v-model="formItem.qz" :min="0" placeholder="" style="width: 200px" />
      </FormItem>

      <FormItem label="职工付费标准" prop="zgffbz">
        <InputNumber v-model="formItem.zgffbz" :min="0" placeholder="" style="width: 200px" />
      </FormItem>

      <FormItem label="居民付费标准" prop="jmffbz">
        <InputNumber v-model="formItem.jmffbz" :min="0" placeholder="" style="width: 200px" />
      </FormItem>
      <FormItem label="例均费用" prop="ljfy">
        <InputNumber v-model="formItem.ljfy" :min="0" placeholder="" style="width: 200px" />
      </FormItem>
      <FormItem label="住院天数标杆值(天)" prop="zytsbgz">
        <InputNumber v-model="formItem.zytsbgz" :min="0" placeholder="" style="width: 200px" />
      </FormItem>
      <FormItem label="高倍率" prop="highMag">
        <InputNumber v-model="formItem.highMag" :min="0" placeholder="" style="width: 200px" />
      </FormItem>
      <FormItem label="低倍率" prop="lowMag">
        <InputNumber v-model="formItem.lowMag" :min="0" placeholder="" style="width: 200px" />
      </FormItem>
      <FormItem label="是否特病单议病组" prop="isSpecial">
        <Switch v-model="formItem.isSpecial" @on-change="change" />
      </FormItem>
      <FormItem label="特殊标签" prop="specialFlag"><Input v-model="formItem.specialFlag" placeholder=""
          style="width: 200px" />
      </FormItem>
    </Form>
    <template #footer>
      <div>
        <Button type="text" @click="modal.show = false">取消</Button>
        <Button type="primary" @click="handleOk">确定</Button>
      </div>
    </template>
  </Modal>
</template>
<script setup>
import http from '@/util/http'
import _ from 'lodash'
let { proxy } = getCurrentInstance()
const emit = defineEmits(['initList'])
let modal = reactive({ show: false, title: '' })
const form = ref()

let formItem = reactive({
  drg_code: '',
  drg_name: '',
  qz: 0,
  zgffbz: 0,
  jmffbz: 0,
  zytsbgz: 0,
  ljfy: 0,
  highMag: 0,
  lowMag: 0,
  isSpecial: false,
  specialFlag: '',
})

const show = (type, data) => {
  form.value.resetFields()
  modal.show = true
  formItem.paraID = ''
  if (type == 1) {
    modal.title = '添加'
  } else {
    var clone = _.cloneDeep(data)
    formItem = clone
    modal.title = '编辑'
  }
}
const validateNum = (rule, value, callback) => {
  if (value === '' || value === null) {
    callback(new Error('请输入值'))
  } else {
    callback()
  }
}

const ruleValidate = {
  drg_code: [{ required: true, message: '请输入', trigger: 'blur' }],
  drg_name: [{ required: true, message: '请输入', trigger: 'blur' }],
  qz: [{ required: true, validator: validateNum, trigger: 'blur' }],
  zgffbz: [{ required: true, validator: validateNum, trigger: 'blur' }],
  jmffbz: [{ required: true, validator: validateNum, trigger: 'blur' }],
  ljfy: [{ required: true, validator: validateNum, trigger: 'blur' }],
  zytsbgz: [{ required: true, validator: validateNum, trigger: 'blur' }],
  highMag: [{ required: true, validator: validateNum, trigger: 'blur' }],
  lowMag: [{ required: true, validator: validateNum, trigger: 'blur' }],
  specialFlag: [{ required: true, message: '请输入', trigger: 'blur' }],
}

const handleOk = () => {
  form.value.validate((valid) => {
    if (valid) {
      http.post('/Basic/DRGPara/AddOrUpdate', toRaw(formItem), (response) => {
        if (response.data.flag) {
          emit('initList')
          proxy.$Message.success(response.data.msg)
          modal.show = false
        } else {
          proxy.$Message.error(response.data.msg)
        }
      })
    } else {
      return false
    }
  })
}

defineExpose({
  show,
})
</script>
